package net.joey.httpclient;

import java.io.IOException;
import java.net.CookieStore;

import org.apache.commons.httpclient.HttpClient;

import com.sun.net.httpserver.HttpContext;

/**
 * @author YangHua 转载请注明出处：http://www.xfok.net/2009/10/124488.html
 */
public class MyClient {

	public void loginRenRen() {
		// 创建默认的httpClient实例.
//		HttpClient httpclient = new DefaultHttpClient();
//		// 创建httppost
//		HttpPost httppost = new HttpPost(
//				"http://www.renren.com/PLogin.do");
//		// 创建参数队列
//		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//		formparams.add(new BasicNameValuePair("email", "maotou528@126.com"));
//		formparams.add(new BasicNameValuePair("password", "wawdxgg923"));
//		UrlEncodedFormEntity uefEntity;
//		try {
//			uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
//			httppost.setEntity(uefEntity);
//			System.out.println("executing request " + httppost.getURI());
//			HttpResponse response;
//			response = httpclient.execute(httppost);
//			HttpEntity entity = response.getEntity();
//			if (entity != null) {
//				System.out.println("--------------------------------------");
//				System.out.println("Response content: "
//						+ EntityUtils.toString(entity, "UTF-8"));
//				System.out.println("--------------------------------------");
//			}
////			HttpGet httpget = new HttpGet(
////					"http://www.renren.com/callback.do?t=0ee7d399eb4483833f47066220d789861&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do&needNotify=false");
////			response = httpclient.execute(httpget);
////			entity = response.getEntity();
////			System.out.println("Response content: "
////					+ EntityUtils.toString(entity, "UTF-8"));
//			
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (UnsupportedEncodingException e1) {
//			e1.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			// 关闭连接,释放资源
//			httpclient.getConnectionManager().shutdown();
//		}

	}

	/**
	 * 的到Http请求结果
	 * 
	 * @param url请求地址
	 * @param parms请求参数
	 * @return
	 * @throws IOException
	 */
	public String getBody(String i) throws IOException {
		return null;
//		if(i.equals("0")){
//			i = "";
//		}
//		System.out.println(Thread.currentThread()+"-----"+i+"####");
//		// 创建默认的httpClient实例.
//		HttpClient httpclient = new DefaultHttpClient();
//		try {
//			// 创建httpget.
//			HttpGet httpget = new HttpGet(
//					"http://wpi.renren.com/comet_get?mid=0&r=" + Math.random());
//			System.out.println("executing request " + httpget.getURI());
//			// 执行get请求.
////			HttpResponse response = httpclient.execute(httpget);
//			
//			CookieStore cookieStore = new BasicCookieStore();
//			BasicClientCookie smdataCookie = new BasicClientCookie("_r01", "1");
//			smdataCookie.setDomain("renren.com");
//			BasicClientCookie smdataCookie1 = new BasicClientCookie("depovince", "BJ");
//			smdataCookie1.setDomain("renren.com");
//			BasicClientCookie smdataCookie2 = new BasicClientCookie("p", "a65ea5eb46cf6ae199a2af84b0a4d75c1");
//			smdataCookie2.setDomain("renren.com");
//			BasicClientCookie smdataCookie3 = new BasicClientCookie("t", "0ee7d399eb4483833f47066220d789861");
//			smdataCookie3.setDomain("renren.com");
//			BasicClientCookie smdataCookie4 = new BasicClientCookie("societyguester", "0ee7d399eb4483833f47066220d789861");
//			smdataCookie4.setDomain("renren.com");
//			BasicClientCookie smdataCookie5 = new BasicClientCookie("id", "44487491");
//			smdataCookie5.setDomain("renren.com");
//			BasicClientCookie smdataCookie6 = new BasicClientCookie("xnsid", "4e351859");
//			smdataCookie6.setDomain("renren.com");
//			BasicClientCookie smdataCookie7 = new BasicClientCookie("wpsid", "13155675986501"+i);
//			smdataCookie7.setDomain("renren.com");
//			cookieStore.addCookie(smdataCookie);
//			cookieStore.addCookie(smdataCookie2);
//			cookieStore.addCookie(smdataCookie3);
//			cookieStore.addCookie(smdataCookie4);
//			cookieStore.addCookie(smdataCookie5);
//			cookieStore.addCookie(smdataCookie6);
//			cookieStore.addCookie(smdataCookie7);
//			
//			HttpContext localContext = new BasicHttpContext();
//			localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);	
//			HttpResponse response = httpclient.execute(httpget, localContext);
//			// 获取响应实体
//			HttpEntity entity = response.getEntity();
//			// 打印响应状态
//			System.out.println(response.getStatusLine());
//			String cont = null ;
//			if (entity != null) {
//				// 打印响应内容长度
//				System.out.println("Response content length: "
//						+ entity.getContentLength());
//				// 打印响应内容
//				 cont = EntityUtils.toString(entity);
//				System.out.println("Response content: "
//						+ cont);
//			}
//			System.out.println(Thread.currentThread()+"----------over--------------------------");
//			return cont;
//		} finally {
//			// 关闭连接,释放资源
//			httpclient.getConnectionManager().shutdown();
//		}
	}

}
